//
//  ConsultantInformation.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/8/1.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "ConsultantInformation.h"
#import "Config.h"
#import "UserApi.h"
#import "Tool+PsychologicalCounselling.h"
#import "TagSelection.h"

@implementation ConsultantInformation
+(ConsultantInformation *)sharedInformation
{
    static ConsultantInformation *information = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        information = [[ConsultantInformation alloc]init];
    });
    return information;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

-(void)setValue:(id)value forKey:(NSString *)key{
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"field"]) {
        NSArray *array = value;
        NSMutableArray *marray = [NSMutableArray new];
        for (NSString *str in array) {
            [marray addObject:@([str intValue])];
        }
        self.fields = marray;
    }
}

-(void)requestInformation:(void(^)(NSError *error))completion
{
    if ([ConsultantInformation sharedInformation].id) {
        completion(nil);
    }
    
    UserApi *api = [[UserApi alloc]init];
    api.needHUD = NO;
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
        if (kCode(request.responseJSONObject)) {
            [self setValuesForKeysWithDictionary:[request.responseJSONObject objectForKey:@"data"]];
            completion(nil);
        }else{
            NSError *error = [NSError errorWithDomain:@"..." code:1111111 userInfo:nil];
            completion(error);
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        completion(request.error);
    }];
}

-(NSString *)consultantTitle
{
    NSString *str = @"";
    NSArray *array = [Tool AllConsultantTitle];
    for (TagSelection* model in array) {
        if (self.counselor_title == model.id) {
            str = model.name;
            break;
        }
    }
    return str;
}

-(NSString *)hospitalTitle
{
    NSString *str = @"";
    NSArray* array = [Tool AllHospitalTitle];
    for (TagSelection* model in array) {
        if (self.hospital_title == model.id) {
            str = model.name;
            break;
        }
    }
    return str;
}

-(NSString *)skillField{
    NSString *str = @"";
    NSArray *array = [Tool AllSkilledfieldTitle];
    for (NSNumber *num in self.fields) {
        for (TagSelection *tag in array) {
            if (num.intValue == tag.id) {
                str = [NSString stringWithFormat:@"%@%@",str,tag.name];
                break;
            }
        }
    }
    return str;
}

@end
